/**
 * 
 */
package com.bitBusy.manageyourriches.database.queries;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import com.bitBusy.manageyourriches.database.contactClasses.PaymentReminder;
import com.bitBusy.manageyourriches.database.tables.PaymentReminders;

/**
 * @author SumaHarsha
 *
 */
public class QueriesPaymentRemindersTable  extends DatabaseQueries{

	/**
	 * @param the_context
	 */
	public QueriesPaymentRemindersTable(Context the_context) {
		super(the_context);
	}
	/** Name of table related to queries in this class*/
	private static final String tableName = PaymentReminders.class.getSimpleName();
	
	/** String for the category column name*/
	private static final String dayCol = PaymentReminders.DAYOFMONTH.getName();
			
	/** String for the category column name*/
	private static final String monthCol = PaymentReminders.MONTH.getName();
	
	/** String for the category column name*/
	private static final String yearCol = PaymentReminders.YEAR.getName();
	
	/** String for the category column name*/
	private static final String hourCol = PaymentReminders.HOUR.getName();
	
	/** String for the category column name*/
	private static final String minCol = PaymentReminders.MINUTE.getName();
	
	/** String for the category column name*/
	private static final String noteCol = PaymentReminders.NOTE.getName();

	private static final String recurrentCol = PaymentReminders.RECURRENT.getName();
	
	private static final String recurrentMonthlyCol = PaymentReminders.MONTHLY.getName();
	
	private static final String emailCol = PaymentReminders.EMAIL.getName();
	
	private static final String idCol = PaymentReminders.UNIQUEID.getName();
	
	private static final int DEFAULTNONRECURRENTVALUE = 0;
				
	private static final int DEFAULTRECURRENTVALUE = 1;

	/** String array of all column names in this table*/
	private static final String[] allColumns = new String[]{dayCol, monthCol, yearCol, hourCol, minCol, noteCol, recurrentCol, recurrentMonthlyCol,
		emailCol, idCol};
		
	/** Reference to the Cursor object used to store and process query results*/
	private Cursor cursor;




	/**
	 * @param i
	 * @param j
	 * @param k
	 * @param l
	 * @param m
	 * @return 
	 */
	public boolean saveAlarm(PaymentReminder the_reminder) {
		boolean iOpened = checkAndOpen();
		boolean returnValue = true;
	ContentValues contentInsert = new ContentValues();
			contentInsert.put(dayCol, the_reminder.getDay());
			contentInsert.put(monthCol, the_reminder.getMonth());
			contentInsert.put(yearCol, the_reminder.getYear());
			contentInsert.put(hourCol, the_reminder.getHour());
			contentInsert.put(minCol, the_reminder.getMinute());
			contentInsert.put(noteCol, the_reminder.getNote());
			contentInsert.put(recurrentCol, the_reminder.isRecurrent());
			contentInsert.put(recurrentMonthlyCol, the_reminder.isMonthly());
			contentInsert.put(emailCol, the_reminder.getEmail());
			contentInsert.put(idCol, the_reminder.getID());

			try
			{
			myDatabase.insertOrThrow(tableName, null, contentInsert);
			}
			catch(Exception sqlex)
			{
		//		DisplayMessages.displayUserError(myActivityContext, DialogBoxPrompts.SQLINSERTTEXCEPTIONPROMPT.getPrompt());
				returnValue = false;
			}		
		
		
		finally
		{
			checkAndClose(iOpened);
		}
			return returnValue;
	
	}




	/**
	 * @param the_reminder
	 */
	public boolean deleteAlarm(PaymentReminder the_reminder) {
		boolean retValue = false;
		if (the_reminder != null)
		{
			boolean iOpened = checkAndOpen();
	
					if (myDatabase.delete(tableName, 
									dayCol + " = " + the_reminder.getDay() +" AND " +
									monthCol + " = " + the_reminder.getMonth() + " AND " + 
									yearCol + " = " + the_reminder.getYear()  + " AND " +
									 hourCol + " = " + the_reminder.getHour()  +" AND " +
									minCol + " = " + the_reminder.getMinute() + " AND "+
									recurrentCol + " = " + the_reminder.isRecurrent() + " AND " +
								     recurrentMonthlyCol + " = " + the_reminder.isMonthly() + " AND "+
								     idCol + " = " + the_reminder.getID(), null)== 1)
									{
										retValue = true;
									}
					
			checkAndClose(iOpened);
		}
		return retValue;

				 
	}




	/**
	 * @return
	 */
	public ArrayList<PaymentReminder> getAllNonRecurrentReminders() {
		boolean iOpened = checkAndOpen();
		ArrayList<PaymentReminder> reminders = new ArrayList<PaymentReminder>();
		cursor = myDatabase.query
		(tableName,allColumns, recurrentCol + "=" + DEFAULTNONRECURRENTVALUE,null, null, null, null);
		if (cursor != null && cursor.moveToFirst())
		{
			while (!cursor.isAfterLast())
			{
				reminders.add(new PaymentReminder(cursor.getInt(cursor.getColumnIndex(dayCol)), cursor.getInt(cursor.getColumnIndex(monthCol)), 
						cursor.getInt(cursor.getColumnIndex(yearCol)), cursor.getInt(cursor.getColumnIndex(hourCol)), cursor.getInt(cursor.getColumnIndex(minCol)),
						cursor.getString(cursor.getColumnIndex(noteCol)), cursor.getInt(cursor.getColumnIndex(recurrentCol)),
								cursor.getInt(cursor.getColumnIndex(recurrentMonthlyCol)),
								cursor.getString(cursor.getColumnIndex(emailCol)), cursor.getInt(cursor.getColumnIndex(idCol))));
								cursor.moveToNext();
			}
			cursor.close();
		}
		checkAndClose(iOpened);
		return reminders;
	}		
	
	/**
	 * @return
	 */
	public ArrayList<PaymentReminder> getAllReminders() {
		boolean iOpened = checkAndOpen();
		
		ArrayList<PaymentReminder> reminders = new ArrayList<PaymentReminder>();
		cursor = myDatabase.query
		(tableName, allColumns, null, null, null, null, null);
		if (cursor != null && cursor.moveToFirst())
		{
			while (!cursor.isAfterLast())
			{
				reminders.add(new PaymentReminder(cursor.getInt(cursor.getColumnIndex(dayCol)), cursor.getInt(cursor.getColumnIndex(monthCol)), 
						cursor.getInt(cursor.getColumnIndex(yearCol)), cursor.getInt(cursor.getColumnIndex(hourCol)), cursor.getInt(cursor.getColumnIndex(minCol)),
						cursor.getString(cursor.getColumnIndex(noteCol)), cursor.getInt(cursor.getColumnIndex(recurrentCol)), 
								cursor.getInt(cursor.getColumnIndex(recurrentMonthlyCol)),
								cursor.getString(cursor.getColumnIndex(emailCol)), cursor.getInt(cursor.getColumnIndex(idCol))));
				cursor.moveToNext();
			}
			cursor.close();
		}
		checkAndClose(iOpened);
		return reminders;
	}




	/**
	 * @return
	 */
	public ArrayList<PaymentReminder> getAllRecurrentReminders() {
boolean iOpened = checkAndOpen();
		
		ArrayList<PaymentReminder> reminders = new ArrayList<PaymentReminder>();
		cursor = myDatabase.query
		(tableName, allColumns, recurrentCol + "=" + DEFAULTRECURRENTVALUE,null, null, null, null);
		if (cursor != null && cursor.moveToFirst())
		{
			while (!cursor.isAfterLast())
			{
				reminders.add(new PaymentReminder(cursor.getInt(cursor.getColumnIndex(dayCol)), cursor.getInt(cursor.getColumnIndex(monthCol)), 
						cursor.getInt(cursor.getColumnIndex(yearCol)), cursor.getInt(cursor.getColumnIndex(hourCol)), cursor.getInt(cursor.getColumnIndex(minCol)),
						cursor.getString(cursor.getColumnIndex(noteCol)), cursor.getInt(cursor.getColumnIndex(recurrentCol)), 
						cursor.getInt(cursor.getColumnIndex(recurrentMonthlyCol)),
						cursor.getString(cursor.getColumnIndex(emailCol)), cursor.getInt(cursor.getColumnIndex(idCol))));
				cursor.moveToNext();
			}
			cursor.close();
		}
		checkAndClose(iOpened);
		return reminders;
	}		
	


}
